<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <title>http://localhost:8080/upload.html</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 40px;
            background-color: #f4f6f9;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            padding: 30px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        h2 {
            text-align: center;
            color: #333;
        }
        label {
            display: block;
            margin-top: 15px;
            font-weight: bold;
            color: #555;
        }
        select, input[type="file"] {
            width: 100%;
            padding: 10px;
            margin-top: 5px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
        }
        button {
            margin-top: 20px;
            padding: 12px 20px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 5px;
            font-size: 14px;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .download-section {
            margin-top: 30px;
            padding: 20px;
            background-color: #f8f9fa;
            border-radius: 8px;
            border: 1px dashed #ccc;
        }
        .download-btn {
            display: inline-block;
            margin: 10px 10px 10px 0;
            padding: 8px 16px;
            background-color: #28a745;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-size: 14px;
        }
        .download-btn:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>访问网址：http://localhost:8080/upload.html</h2>

    <label for="type">选择数据类型：</label>
    <select id="type" required>
        <option value="">-- 请选择 --</option>
        <option value="bd_sub_product_type">主线订单-电驱打刻配置</option>
        <option value="online_bom">主线订单-BOM</option>
        <option value="online_product">主线订单-工单主体信息</option>
    </select>

    <label for="file">上传 Excel 文件 (.xlsx)：</label>
    <input type="file" id="file" accept=".xlsx" required />

    <button onclick="uploadFile()">开始上传</button>

    <div id="result"></div>

    <!-- 下载模板区域 -->
    <div class="download-section">
        <h3>📥 下载模板</h3>
        <a href="/templates/主线订单-电驱打刻配置.xlsx" download class="download-btn">主线订单-电驱打刻配置</a>
        <a href="/templates/主线订单-BOM.xlsx" download class="download-btn">主线订单-BOM</a>
        <a href="/templates/主线订单-工单主体信息.xlsx" download class="download-btn">主线订单-工单主体信息</a>
    </div>
</div>

<script>
    function uploadFile() {
        const typeSelect = document.getElementById('type');
        const fileInput = document.getElementById('file');
        const resultDiv = document.getElementById('result');

        const type = typeSelect.value;
        const file = fileInput.files[0];

        if (!type) {
            alert('请先选择数据类型！');
            return;
        }
        if (!file) {
            alert('请选择一个文件！');
            return;
        }

        const formData = new FormData();
        formData.append('type', type);
        formData.append('file', file);

        // 显示加载中
        resultDiv.className = '';
        resultDiv.textContent = '正在上传...';

        fetch('/api/excel/import', {
            method: 'POST',
            body: formData
        })
            .then(res => res.text())
            .then(text => {
                resultDiv.textContent = text;
                if (text.includes('成功')) {
                    resultDiv.className = 'result success';
                } else {
                    resultDiv.className = 'result error';
                }
            })
            .catch(err => {
                resultDiv.className = 'result error';
                resultDiv.textContent = '上传失败: ' + err.message;
            });
    }
</script>
</body>
</html>